﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HodhodNews;
using Googler.DAL;
using System.Web;
using GlobalResources;
using System.ComponentModel;
using System.Diagnostics;
using Googler.BLL;
using HodhodNews.GlobalResources;

namespace Googler.Controls
{

    public class NewsViewer : WebBrowser
    {
        public NewsViewer()
        {
            this.Mode = NewsViewerMode.MultipleItems;
        }

        public NewsViewerMode Mode { get; set; }
        public List<News> NewsSource { get; set; }

        INewsRenderer _NewsRenderer;
        INewsRenderer NewsRenderer
        {
            get
            {
                if (_NewsRenderer == null)
                {
                    _NewsRenderer = new NewsRenderer();
                }
                return _NewsRenderer;
            }
        }

        [Category("Action")]
        public event NewsEventHandler NewsDeleting;

        [Category("Action")]
        public event NewsEventHandler NewsPreviewing;

        public virtual void DataBind()
        {
            if (NewsSource == null || NewsSource.Count == 0)
            {
                this.DocumentText = "";
                return;
            }
            switch (this.Mode)
            {
                case NewsViewerMode.SingleItem:
                    this.DocumentText = this.NewsRenderer.Render(NewsSource.First(),true);
                    break;
                default:
                    this.DocumentText = this.NewsRenderer.Render(NewsSource);
                    break;
            }
        }
        

        protected override void OnNavigating(WebBrowserNavigatingEventArgs e)
        {
            base.OnNavigating(e);
            if (e.Url.AbsoluteUri == "about:blank")
            {
                return;
            }
            e.Cancel = true;

            var newsCommand = this.NewsRenderer.ParseCommand(e.Url);

            if (newsCommand.Type == NewsCommandType.GoingToGoogle)
            {
                var process = new Process();
                process.StartInfo.CreateNoWindow = false;
                process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
                process.StartInfo.FileName = "iexplore";
                process.StartInfo.Arguments = e.Url.AbsoluteUri;
                process.Start();
                return;
            }
            if (newsCommand.OId <= 0)
            {
                MessageBox.Show(Messages.InvalidNewsID);
            }


            if (newsCommand.Type == NewsCommandType.Delete)
            {
                if (NewsDeleting != null)
                {
                    NewsDeleting(this, new NewsEventArgs(newsCommand.OId));
                }
                this.DataBind();
            }
            else if (newsCommand.Type == NewsCommandType.Preview)
            {
                if (NewsPreviewing != null)
                {
                    NewsPreviewing(this, new NewsEventArgs(newsCommand.OId));
                }
            }
        }
    }
}
